home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cug236.zip / HEAD.C < prev    next >
Text File  |  1980-01-02  |  4KB  |  132 lines

  1. /*
  2.     HEADER:        CUG000.00;
  3.     TITLE:        List Head of Text Files;
  4.     DATE:        04/04/1987;
  5.     DESCRIPTION:    "Lists the first n lines of each of a list of files.  n
  6.             is variable as is the tab increment of the listing.";
  7.     VERSION:    2.0;
  8.     FILENAME:    HEAD.C;
  9.     COMPILERS:    vanilla;
  10.     AUTHORS:    W. C. Colley III, E. H. Mallory;
  11. */
  12.  
  13. /*
  14.     Utility to Print the First n Lines of a List of Files
  15.  
  16.     Based upon Eugene H. Mallory's HEAD.C (copyright 1983)
  17.  
  18.     Rewrite by William C. Colley, III -- 4 APR 1987
  19.  
  20. The purpose of this rewrite is to get a portable version of HEAD.C as the
  21. original was coded in BDS C.  This involves some loss of functionality as the
  22. BDS C function wildexp() is irrevokably CP/M dependent.     The differences are:
  23.  
  24.     1)  Wild-carded file names cannot be expanded unless the host operating
  25.         system provides this facility as part of command-line argument
  26.         passing.  UNIX does, MSDOS and CP/M do not.
  27.  
  28.     2)  I punted the sorting of the argument list.    This allows the -
  29.         options to apply to particular files.  Thus, if you want to see the
  30.         first 10 lines of FOO.C and the first 20 lines of BAR.C, you can
  31.         do it with the following command:
  32.  
  33.         HEAD -L10 FOO.C -L20 BAR.C
  34.  
  35.     3)  The -H option has been dropped to simplify things.    If you type
  36.         the command HEAD with no arguments, you will still get the usage
  37.         reminder.
  38.  
  39.     4)  Only the first WIDTH characters of a line are displayed.  The rest
  40.         are sent to the bit bucket.     The usual procedure would be to set
  41.         WIDTH to the width of your terminal or printer.
  42.  
  43.     5)  Controllable tab expansion size has been added with the -T option.
  44.         Thus, you can expand FOO.C's tabs on 4-space intervals with the
  45.         following command:
  46.  
  47.         HEAD -T4 FOO.C
  48.  
  49.     6)  Bit 7 is stripped on all displayed characters so that WordStar
  50.         images and the like don't generate funny graphics junk on some
  51.         terminals.
  52. */
  53.  
  54. #include <stdio.h>
  55.  
  56. /*  Uncomment the following #define if you are using an AZTEC C compiler.  */
  57.  
  58. /*
  59. #define getc(x)        agetc(x)
  60. */
  61.  
  62. /*  User-adjustable default settings:                    */
  63.  
  64. #define WIDTH        79    /* Max length of line to display.    */
  65. #define HEADLEN        10    /* Default length of head to display.    */
  66. #define TABSIZE        8    /* Default tab size.            */
  67.  
  68. int main(argc,argv)
  69. int argc;
  70. char **argv;
  71. {
  72.     char *bp, buf[WIDTH + 1];
  73.     int i, j;
  74.     FILE *f;
  75.     static char opts[] = "[ -Lnn | -Tnn | <filename> ]";
  76.     static int err = 0, headlen = HEADLEN, tabsize = TABSIZE;
  77.     int atoi();
  78.  
  79.     if (argc < 2) {
  80.     printf("Usage:\tHEAD %s %s*\n",opts,opts);
  81.     err = !0;
  82.     }
  83.     else while (--argc) {
  84.     if (**++argv == '-') {
  85.         i = *(*argv + 1);
  86.         switch (i) {
  87.         case 'l':
  88.         case 'L':   if ((i = atoi(*argv + 2)) < 1) {
  89.                 printf("ERROR:  Head length < 1\n\n");
  90.                 err = !0;
  91.                 }
  92.                 else headlen = i;
  93.                 break;
  94.  
  95.         case 't':
  96.         case 'T':   if ((i = atoi(*argv + 2)) < 1) {
  97.                 printf("ERROR:  Tab size < 1\n\n");
  98.                 err = !0;
  99.                 }
  100.                 else tabsize = i;
  101.                 break;
  102.  
  103.         default:    printf("ERROR:  Illegal option %s\n\n",*argv);
  104.                 err = !0;  break;
  105.         }
  106.     }
  107.     else {
  108.         if (!(f = fopen(*argv,"r"))) {
  109.         printf("ERROR:  Cannot find file %s\n\n",*argv);
  110.         err = !0;
  111.         }
  112.         else {
  113.         printf("FILE:   %s\n\n",*argv);
  114.         for (i = headlen; i && !feof(f); --i) {
  115.             for (bp = buf; ; ) {
  116.             if ((j = getc(f)) == EOF || (j &= 0177) == '\n') break;
  117.             if (j == '\t')
  118.                 while (bp < buf + WIDTH) {
  119.                 *bp++ = ' ';
  120.                 if (!((bp - buf) % tabsize)) break;
  121.                 }
  122.             else if (bp < buf + WIDTH) *bp++ = j;
  123.             }
  124.             *bp = '\0';     printf("%s\n",buf);
  125.         }
  126.         printf("\n");  fclose(f);
  127.         }
  128.     }
  129.     }
  130.     return err;
  131. }
  132.